package com.example.customviewtest.prize_game;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.customviewtest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @auther XuDan on 2020/6/22.
 */
public class NineLuckPan extends FrameLayout {


    private ArrayList<PrizeBean> mPrizeBeans;
    private GridAdapter mAdapter;

    public NineLuckPan(Context context) {
        this(context, null);
    }

    public NineLuckPan(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NineLuckPan(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.prize_layout, this, true);
        GridView grid_view = view.findViewById(R.id.grid_view);
        initData();
        mAdapter = new GridAdapter(context, mPrizeBeans);
        grid_view.setAdapter(mAdapter);
        grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 4) {
                    startAnim(randomNum(0, 8));
                }
            }
        });
    }

    private int startLuckPosition = 0;

    private void startAnim(int luckNum) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(startLuckPosition, 8).setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int position = (int) animation.getAnimatedValue();
                for (int i = 0; i < mPrizeBeans.size(); i++) {
                    PrizeBean prizeBean = mPrizeBeans.get(i);
                    if (prizeBean.index == position) {
                        prizeBean.select_bg = Color.RED;
                    } else {
                        prizeBean.select_bg = Color.TRANSPARENT;
                    }
                }
                mAdapter.notifyDataSetInvalidated();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startLuckPosition = luckNum;
                for (int i = 0; i < mPrizeBeans.size(); i++) {
                    PrizeBean prizeBean = mPrizeBeans.get(i);
                    if (prizeBean.index == luckNum) {
                        prizeBean.select_bg = Color.RED;
                    } else {
                        prizeBean.select_bg = Color.TRANSPARENT;
                    }
                }
                mAdapter.notifyDataSetInvalidated();
            }
        });
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.start();
    }

    /**
     * 生成随机数
     *
     * @param minNum
     * @param maxNum
     * @return
     */
    private int randomNum(int minNum, int maxNum) {
        int max = maxNum;
        int min = minNum;
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mPrizeBeans = new ArrayList<>();
        mPrizeBeans.add(new PrizeBean(0, R.drawable.sample_footer_loading, Color.GREEN, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(1, R.drawable.sample_footer_loading, Color.YELLOW, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(2, R.drawable.sample_footer_loading, Color.GREEN, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(7, R.drawable.sample_footer_loading, Color.YELLOW, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(8, R.drawable.sample_footer_loading, Color.WHITE, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(3, R.drawable.sample_footer_loading, Color.YELLOW, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(6, R.drawable.sample_footer_loading, Color.GREEN, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(5, R.drawable.sample_footer_loading, Color.YELLOW, Color.TRANSPARENT));
        mPrizeBeans.add(new PrizeBean(4, R.drawable.sample_footer_loading, Color.GREEN, Color.TRANSPARENT));
    }

    static class GridAdapter extends BaseAdapter {
        Context mContext;
        List<PrizeBean> list;


        public GridAdapter(Context context, ArrayList<PrizeBean> prizeBeans) {
            this.mContext = context;
            this.list = prizeBeans;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.prize_item_layout, null);
                holder = new ViewHolder();
                holder.img = convertView.findViewById(R.id.prize_item_img);
                holder.bg = convertView.findViewById(R.id.prize_item_bg);
                holder.select_bg = convertView.findViewById(R.id.fl_bg);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            PrizeBean prizeBean = list.get(position);
            holder.img.setImageResource(prizeBean.img_id);
            holder.bg.setBackgroundColor(prizeBean.getBg());
            holder.select_bg.setBackgroundColor(prizeBean.select_bg);
            return convertView;
        }

        public static class ViewHolder {
            ImageView img;
            FrameLayout bg;
            FrameLayout select_bg;
        }
    }
}
